#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;

class ServeInfo
{
public:
	ServeInfo(const char* ip = "0.0.0.0", int port = 443)
		:_ip(ip)
		,_port(port)
	{}

	string _ip;
	int _port;
};

class ConfigManager
{
public:
	ConfigManager(const char* filename = "test.bin")
		:_filename(filename)
	{}
	void WriteBin(const ServeInfo& info)
	{
		ofstream ofs(_filename, ios::out | ios::binary);
		ofs.write((const char*)&info, sizeof(ServeInfo));
		ofs.close();
	}

	void ReadBin(ServeInfo& info)
	{
		ifstream ifs(_filename, ios::in | ios::binary);
		ifs.read((char*)&info, sizeof(ServeInfo));
		ifs.close();
	}

	void WriteTxt(const ServeInfo& info)
	{
		ofstream ofs(_filename,ios::out | ios::app);
		ofs << info._ip << endl;
		ofs << info._port << endl;
		ofs.close();
	}

	void ReadTxt(ServeInfo& info)
	{
		ifstream ifs(_filename);
		ifs >> info._ip;
		ifs >> info._port;
		ifs.close();
	}

	void Print()
	{

	}
private:
	string _filename;
};

void Test1()
{
	ServeInfo info("127.0.0.1",80);
	ConfigManager cm;
	cm.WriteBin(info);
	//cout << info._ip << ":" << info._port;
}

void Test2()
{
	ServeInfo info;
	ConfigManager cm;
	cm.ReadBin(info);
	cout << info._ip << ":" << info._port;
}

void Test3()
{
	ServeInfo info("127.0.0.1",80);
	ConfigManager cm("test.txt");
	cm.WriteTxt(info);
	//cout << info._ip << ":" << info._port;
}

void Test4()
{
	ServeInfo info;
	ConfigManager cm("test.txt");
	cm.ReadTxt(info);
	cout << info._ip << ":" << info._port;
}

void Test5()
{
	stringstream ss;
	ss << 123456;
	ss << "abcdefg\nabcdefg\n";
	string s;
	ss >> s;
	ss >> s;
	cout << ss.str();
	ss.str("");
	ss.clear();
	cout << s.c_str();
}

int main()
{                                                
	//Test1();
	//Test2();
	Test3();
	//Test4();
	//Test5();
	string s;
	ServeInfo info;
	ifstream f("test.txt", ios::in);
	while (f >> info._ip >> info._port)
		cout << info._ip << ":" << info._port << endl;
	return 0;
}